package cn.com.dhee.springcloudtraining.user.controller;

import cn.com.dhee.springcloudtraining.order.api.qo.OrderQO;
import cn.com.dhee.springcloudtraining.order.api.vo.OrderVO;
import cn.com.dhee.springcloudtraining.user.api.vo.UserOrder;
import cn.com.dhee.springcloudtraining.user.api.vo.UserOrderInfo;
import cn.com.dhee.springcloudtraining.user.api.vo.UserVO;
import cn.com.dhee.springcloudtraining.user.feign.OrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ralphwong
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private OrderFeign orderFeign;
    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public UserVO get(@PathVariable("id") Long userId){
        UserVO vo = new UserVO();
        vo.setUserId(System.currentTimeMillis());
        vo.setUserName("用户名");
        return vo;
    }

    /**
     * 获取当前用户的订单信息
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/orders")
    public UserOrderInfo orders() throws InterruptedException {
        Long currentUserId = System.currentTimeMillis();
        List<UserOrder> list = new ArrayList<>();
        OrderQO orderQO = new OrderQO();
        List<OrderVO> orderList1 = orderFeign.select(orderQO);
        convert2UserOrder(orderList1,list);
        List<OrderVO> orderList2 = orderFeign.select(orderQO);
        convert2UserOrder(orderList2,list);

        UserOrderInfo userOrderInfo = new UserOrderInfo();
        userOrderInfo.setOrderList(list);
        return userOrderInfo;
    }

    private List<UserOrder> convert2UserOrder(List<OrderVO> orderList,List<UserOrder> userOrderList){
        for (OrderVO vo: orderList) {
            UserOrder userOrder = new UserOrder();
            userOrder.setOrderNo(vo.getOrderNo());
            userOrder.setOrderDate(vo.getOrderDate());
            userOrder.setRemark(vo.getRemark());
            userOrderList.add(userOrder);
        }
        return userOrderList;
    }
}
